package yxy.game.pm2.command.hero;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_武将直升活动;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.pubsub.HeroStarPublisher;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.resonate.ResonateHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.HashSet;
import java.util.List;

@MessageType(proto = Command.HeroStarUp, classtype = HeroMessage.HeroStarUpRequest.class)
public class CMD_HeroStarUp extends Command<HeroMessage.HeroStarUpRequest> {

    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroStarUpRequest request) {
        String playerCode = messageSource.getTag();
        String code = request.getCode();
        //消耗武将的code
        List<String> expend = request.getExpendList();

        HeroMessage.HeroStarUpResponse.Builder response = HeroMessage.HeroStarUpResponse.newBuilder();

        try {
            if (Hero.lockHero(playerCode) && Soul.lockSoul(playerCode)) {
                HeroHandle heroHandle = new HeroHandle();
                Hero hero = heroHandle.getHero(playerCode, code);
                if (hero == null) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                //共鸣的武将不能用作升星材料
                Result resultForResonate = ResonateHandle.heroIsResonateHero(expend, playerCode);
                if(!resultForResonate.equals(Result.Success)){
                    send(messageSource, response, resultForResonate);
                    return;
                }


                Result result = heroHandle.starUp(playerCode, hero, new HashSet<>(expend), response);
                send(messageSource, response, result);

                if (result.equals(Result.Success)) {
                    //武将直升活动开启，且是活动的武将，则任务提交
                    actstartupdataIsOpen(playerCode, hero);
                    //发布
                    HeroStarPublisher.getInstance().publish(playerCode, CCReason.武将培养, hero);

                    //判断如果消耗的是共鸣系统场上的武将，对应操作
                    ResonateHandle.heroIsAboveHeroAndResolve(expend, playerCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            Hero.unlockHero(playerCode);
            Soul.unlockSoul(playerCode);
        }

    }

    private void send(MessageSource messageSource, HeroMessage.HeroStarUpResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    /**
     * 判断武将直升活动，如果开启就对应判断武将是否升星
     * @param playerCode
     * @param hero
     */
    private void actstartupdataIsOpen(String playerCode, Hero hero) {

        ActiveModule_武将直升活动 am = ActiveModule_武将直升活动.get(playerCode, false);

        if (am == null) {
            return;
        }

        String heroid_prefix = String.valueOf(am.getHeroId()).substring(0, 5);//id前缀

        if (!String.valueOf(hero.getDataId()).startsWith(heroid_prefix)) {
            return;
        }

        if (!am.running()) {
            return;
        }
        TaskHandle.handle(playerCode, TaskType.指标型_武将星级_达到, hero.getStar());
    }
}
